package com.lazy.utils.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * 日期工具类、jdk 1.8 time
 * 【线程安全】
 *
 * @author zk
 * @date  2018/1/23
 */
public class LocalDateTimeUtil {

    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMM = "yyyyMM";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String YYYY = "yyyy";

    public static final String YYYY_MM_DD_TO_CHINA = "yyyy年MM月dd日";

    /**
     * 按格式转化时间
     *
     * @param localDateTime
     * @param pattern
     * @return
     */
    public static String format(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 判断日期是否是昨天
     *
     * @param localDate
     * @return
     */
    public static boolean isYesterday(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now();
        LocalDate yesterdayDate = nowDate.minusDays(1);
        if (yesterdayDate.equals(localDate)) {
            return true;
        }
        return false;

    }

    /**
     * 判断日期是否为今天
     *
     * @param localDate
     * @return
     */
    public static boolean isToday(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now();
        if (nowDate.equals(localDate)) {
            return true;
        }
        return false;

    }

    /**
     * 判断日期是否在当周内
     *
     * @param localDate
     * @return
     */
    public static boolean isThisWeek(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now();
        LocalDate nowWeekFirstDay = nowDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate nowWeekLastDay = nowDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        if ((localDate.isAfter(nowWeekFirstDay) && localDate.isBefore(nowWeekLastDay)) || localDate.equals(nowWeekFirstDay) || localDate.equals(nowWeekLastDay)) {
            return true;
        }
        return false;
    }

    /**
     * 判断日期是否在上一周
     *
     * @param localDate
     * @return
     */
    public static boolean isPreviousWeek(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now();
        LocalDate previousWeekDate = nowDate.minusWeeks(1);
        LocalDate previousWeekFirstDay = previousWeekDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate previousWeekLastDay = previousWeekDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        if ((localDate.isAfter(previousWeekFirstDay) && localDate.isBefore(previousWeekLastDay)) || localDate.equals(previousWeekFirstDay) || localDate.equals(previousWeekLastDay)) {
            return true;
        }
        return false;
    }

    /**
     * 判断日期是否在当前月内
     *
     * @param localDate
     * @return
     */
    public static boolean isThisMonth(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now();
        LocalDate nowMonthFirstDay = nowDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate nowMonthLastDay = nowDate.with(TemporalAdjusters.lastDayOfMonth());
        if ((localDate.isAfter(nowMonthFirstDay) && localDate.isBefore(nowMonthLastDay)) || localDate.equals(nowMonthFirstDay) || localDate.equals(nowMonthLastDay)) {
            return true;
        }
        return false;
    }

    /**
     * 判断日期是否在上个月内
     *
     * @param localDate
     * @return
     */
    public static boolean isPreviousMonth(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now();
        LocalDate previousMonthDate = nowDate.minusMonths(1);
        LocalDate previousMonthFirstDay = previousMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate previousMonthLastDay = previousMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        if ((localDate.isAfter(previousMonthFirstDay) && localDate.isBefore(previousMonthLastDay)) || localDate.equals(previousMonthFirstDay) || localDate.equals(previousMonthLastDay)) {
            return true;
        }
        return false;
    }

    /**
     * Date 根据时区转化 LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime UDateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * LocalDateTime 转化 Date
     *
     * @param localDateTime
     * @return
     */
    public static Date LocalDateTimeToUDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zoneId).toInstant();
        return Date.from(instant);
    }

    /**
     * 转换成为指定格式
     * 格式必须为 yyyy-MM-dd 或yyyy-MM-dd HH:mm:ss
     * @param date
     * @param localDateFormat
     * @return
     */
    public static String DateStrToLocalDateTime(String date, String localDateFormat) {
        if (date.length()==10){
            LocalDate parse = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            return parse.format(DateTimeFormatter.ofPattern(localDateFormat));
        }else {
            LocalDateTime parse = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return format(parse,localDateFormat);
        }

    }

    /**
     * 获取当前时间 格式为 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurYmdHms(){
        return LocalDateTimeUtil.format(LocalDateTime.now(), YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 获取当前时间 格式为 yyyy年MM月dd日
     * @return
     */
    public static String getCurYmdChina(){
        return LocalDateTimeUtil.format(LocalDateTime.now(), YYYY_MM_DD_TO_CHINA);
    }


    public static void main(String[] args) throws InterruptedException {
//        LocalDateTime localDateTime = LocalDateTime.now();
//        String sss = LocalDateTimeUtil.format(localDateTime, LocalDateTimeUtil.YYYYMMDDHHMMSS);
//
//        System.out.println(sss);
        System.out.println(DateStrToLocalDateTime("2018-06-07 12:12:12",YYYYMMDDHHMMSS));
    }

}
